package cameragps;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;

public class EnterEmailScreen extends PopupScreen {

	TextField emailTextField;

	/**
	 * This screen asks user to enter his email to use it to upload images to
	 * the server
	 */
	public EnterEmailScreen(String email, boolean isNext) {
		super(new VerticalFieldManager());
		LabelField labelField = new LabelField("Enter Email",
				Field.FIELD_HCENTER);
		labelField.setMargin(20, 10, 20, 10);
		add(labelField);

		emailTextField = new TextField(TextField.EDITABLE | Field.FIELD_HCENTER) {

			protected synchronized void paint(Graphics aGraphics) {
				aGraphics.setColor(Color.BLACK);
				super.paint(aGraphics);
			};

			protected boolean keyChar(char key, int status, int time) {
				if (key == Characters.ENTER)
					return true;
				return super.keyChar(key, status, time);
			};
		};

		emailTextField.setBorder(BorderFactory.createBitmapBorder(new XYEdges(
				14, 14, 14, 14), Bitmap.getBitmapResource("bg.png")));
		emailTextField.setMargin(10, 20, 20, 20);
		emailTextField.setPadding(5, 5, 5, 5);
		emailTextField.setText(email);

		add(emailTextField);

		HorizontalFieldManager actionsFieldManager = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		actionsFieldManager.setMargin(10, 10, 10, 10);

		String actionText = "OK";
		if (isNext)
			actionText = "Next";

		ButtonField btnfldOk = new ButtonField(actionText,
				ButtonField.CONSUME_CLICK);
		btnfldOk.setMinimalWidth(100);
		btnfldOk.setMargin(10, 10, 10, 10);
		btnfldOk.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				ImageUtils.saveAccount(emailTextField.getText());
				close();

			}
		});
		actionsFieldManager.add(btnfldOk);

		ButtonField btnfldCancel = new ButtonField("Cancel",
				ButtonField.CONSUME_CLICK);
		btnfldCancel.setMargin(10, 10, 10, 10);
		btnfldCancel.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				close();
			}
		});
		actionsFieldManager.add(btnfldCancel);

		add(actionsFieldManager);
	}
	protected boolean keyChar(char c, int status, int time) {
		if (c == Characters.ESCAPE)
			close();
		return super.keyChar(c, status, time);
	}

}
